package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.business.tb_app_log.TbAppLogService;
import com.tju.resource.business.tb_impl_order.TbImplOrderService;
import com.tju.resource.common.ServiceException;
import com.tju.resource.model.TbImplOrder;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.security.Principal;
import java.util.Map;
import java.util.Objects;

/**
 * 施工单
 */
@RestController
public class TbImplOrderController {
    private final static String prefix = "construction/";
    @Autowired
    private TbImplOrderService tbImplOrderService;
    @Autowired
    private TbAppLogService tbAppLogService;

    /**
     * 查询施工单
     *
     * @param params 查询参数
     * @return 返回施工单列表
     */
    @Log(type= FunctionType.List)
    @PostMapping(prefix + "list")
    @PreAuthorize("hasAnyAuthority('sys:construction:list','sys:cons:construction:list')")
    public Result<Object> selectTbImplOrder(@RequestBody Map<String, String> params, Principal principal) {
        boolean result =false;
        //查询该用户权限
        for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
            if (authority.getAuthority().equals("sys:cons:construction:allList") || authority.getAuthority().equals("sys:construction:allList")) {
                result =true;
                break;
            }
        }
        if(!result){
            params.put("userName", principal.getName());
        }
        return tbImplOrderService.selectTbImplOrder(params);
    }

    /**
     * 添加施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Log(type= FunctionType.Add)
    @PostMapping(prefix + "add")
    @PreAuthorize("hasAnyAuthority('sys:construction:add','sys:cons:construction:add')")
    public Result<Object> insetTbImplOrder(@Valid @RequestBody TbImplOrder tbImplOrder, BindingResult result) {
        try {

            if (result.hasErrors()) {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
            }
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        return tbImplOrderService.insetTbImplOrder(tbImplOrder);
    }

    /**
     * 修改施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Log(type= FunctionType.Modify)
    @PostMapping(prefix + "edit")
    @PreAuthorize("hasAnyAuthority('sys:construction:edit','sys:cons:construction:edit')")
    public Result<Object> updateTbImplOrder(@Valid @RequestBody TbImplOrder tbImplOrder, BindingResult result) {
        try {
            if (result.hasErrors()) {
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
            }
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        return tbImplOrderService.updateTbImplOrder(tbImplOrder);
    }

    /**
     * 删除施工单
     *
     * @param tbImplOrder 施工单内容
     * @return 结果
     */
    @Log(type= FunctionType.Delete)
    @PostMapping(prefix + "delete")
    @PreAuthorize("hasAnyAuthority('sys:construction:delete','sys:cons:construction:delete')")
    public Result<Object> deleteTbImplOrder(@RequestBody TbImplOrder tbImplOrder) {
        return tbImplOrderService.deleteTbImplOrder(tbImplOrder);
    }

    /**
     * 领班人审核
     *
     * @param tbImplOrder 领班人
     * @return
     */
    @Log(type= FunctionType.FirstInstance)
    @PostMapping(prefix + "approve")
    @PreAuthorize("hasAnyAuthority('sys:construction:approve','sys:cons:construction:approve')")
    public Result<Object> approveTbImplOrder(@RequestBody TbImplOrder tbImplOrder) {
        return tbImplOrderService.approveTbImplOrder(tbImplOrder);
    }

    /**
     * 二审核
     *
     * @param tbImplOrder
     * @return
     */
    @Log(type= FunctionType.SecondInstance)
    @PostMapping(prefix + "twoApprove")
    @PreAuthorize("hasAnyAuthority('sys:plan:construction:twoApprove','sys:cons:construction:twoApprove')")
    public Result<Object> twoApproveTbImplOrder(@RequestBody TbImplOrder tbImplOrder) {
        return tbImplOrderService.twoApproveTbImplOrder(tbImplOrder);
    }

    /**
     * 三审核
     *
     * @param tbImplOrder
     * @return
     */
    @Log(type= FunctionType.ThirdTrial)
    @PostMapping(prefix + "threeApprove")
    @PreAuthorize("hasAnyAuthority('sys:plan:construction:threeApprove','sys:cons:construction:threeApprove')")
    public Result<Object> threeApproveTbImplOrder(@RequestBody TbImplOrder tbImplOrder) {
        return tbImplOrderService.threeApproveTbImplOrder(tbImplOrder);
    }

    /**
     * 查询审核记录
     */
    @Log(type= FunctionType.AuditRecords)
    @PostMapping(prefix + "selectApprove")
    @PreAuthorize("hasAnyAuthority('sys:plan:construction:auditRecords','sys:cons:construction:auditRecords')")
    public Result<Object> selectApprove(@RequestBody Map<String, String> map) {
        return tbAppLogService.selectTbAppLogByImplOrderId(map);
    }
}
